package com.gotop.common.config.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @program: cem
 * @description: 拦截检测
 * @author: superfish
 * @create: 2020-12-21 18:38
 **/
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Autowired
    private AuthClientInterceptor authClientInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authClientInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(secureRegistry().getExcludePatterns());
    }

    @Bean
    public SecureRegistry secureRegistry() {
        SecureRegistry secureRegistry = new SecureRegistry();
        secureRegistry.setEnabled(true);
        secureRegistry.excludePathPatterns("/authorization/**");
        secureRegistry.excludePathPatterns("/crontab/**");
        secureRegistry.excludePathPatterns("/actuator/**");
        secureRegistry.excludePathPatterns("/xxl-job-admin/**");
        secureRegistry.excludePathPatterns("/menu/auth-routes");
        secureRegistry.excludePathPatterns("/tenant/info");
        secureRegistry.excludePathPatterns("/doc.html");
        secureRegistry.excludePathPatterns("/js/**");
        secureRegistry.excludePathPatterns("/webjars/**");
        secureRegistry.excludePathPatterns("/swagger-resources/**");
        return secureRegistry;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}